<?php
/**
 * Description of Validations
 *
 * @author Martin Grossmann <snake.aas@gmail.com>
 * @version 1.0
 * @package Validation
 */
class Validations {
  
  const regexp_idPhoto  = null;//"/^[a-zA-Z0-9._ ]*$/"; 
  const regexp_albumName  = null;//"/^[a-zA-Z0-9._ ]*$/";
  const regexp_MimeType = "/^(application|audio|example|image|message|model|multipart|text|video|x-world)\/[a-zA-Z0-9]+([+.-][a-zA-z0-9]+)*$/";
  const regexp_icq      = "/[0-9]{5,10}/";
  const regexp_UserName = "/^[a-zA-Z0-9._ ]*$/";
  const regexp_EMail    = "/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/";
  const regexp_Jabber   = "/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/";
  const regexp_idStyle  = "/^[a-zA-Z0-9._ ]*$/";
  const regexp_dateTime = "/^((((31\.(0?[13578]|1[02]))|((29|30)\.(0?[1,3-9]|1[0-2])))\.(1[6-9]|[2-9]\d)?\d{2})|(29\.0?2\.(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\.((0?[1-9])|(1[0-2]))\.((1[6-9]|[2-9]\d)?\d{2})) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$/";
  const regexp_fileName = "/^[a-z0-9]+\.[a-z]{3,5}$/";
  const regexp_language = "/^[a-z]{2}_[A-Z]{2}$/";
  const regexp_url      = "/^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?$/";
  const regexp_gender   = "/^(male|female)$/";


  const dateOutputFormat = 'Y-m-d H:i:s';
    
  /**
   *
   * @param string $string
   * @param int $length
   * @param string $regexp
   * @param bool $allowNull
   * @return string 
   */
  public static function validateString($string, $length = null, $regexp = null, $allowNull = false) {
    if ($allowNull && $string === null) return null;
    
    if (!is_string($string)) { 
      throw new DomainException($string.' '.T_("is not a correct string value"));
    }  
    
    if ( ($length !== null) && (strlen($string) > $length) ) {
      throw new LengthException($string.' '.T_("is too long"));        
    }  
      
    if ( (!$allowNull) && (empty($string)) ) {   
      throw new LengthException(' '.T_("cannot be empty"));        
    }  
    
    if ( ($regexp !== null) && (!preg_match($regexp, $string)) ) {    
      throw new UnexpectedValueException(T_("Disallowed value").' '.$string);
    }
    
    return htmlspecialchars($string);
    //return $string;
  }
  
  /**
   *
   * @param int $int
   * @param int $min
   * @param int $max
   * @param bool $allowNull
   * @return int
   */
  public static function validateInteger($int, $min = null, $max = null,  $allowNull = false) {
    if ($allowNull && $int === null) return null;
    
    if(!is_numeric($int)) 
      throw new DomainException($int.T_(" must be a number"));
    
    if ( ($min !== null) && ($int < $min) )       
      throw new LengthException(T_("Disallowed value ").$int);
    
    if ( ($max !== null) && ($int > $max) )       
      throw new LengthException(T_("Disallowed value ").$int);
    
    return $int;
  }
  
  /**
   *
   * @param Object $object
   * @param string $className
   * @return Object 
   */
  public static function ValidateObject($object, $className, $allowNull = false) {
    if ($allowNull && $object === null) return null;
    
    if (!is_a($object, $className))
      throw new DomainException(sprintf(T_("Incorrect type - must be of %s"), $className));
    
    return $object;
  }
 
  /**
   * 
   * @return boolean
   * @param mixed $value 
   */
  public static function ValidateBoolean($value) {
    if (!is_bool($value)) 
      throw new DomainException(T_("Value must be boolean"));
    
    return $value;
  }

  public static function ValidateFloat($value) {
    if(!is_float($value)) 
      throw new DomainException(T_("Value must be float"));
    
    return $value;
  }


}

?>
